<?php

/**
 * 买家积分相关操作
 */

namespace Home\Controller;

class MyintegralController extends BaseMemberController {

    function _initialize() {
        parent::_initialize();
        $this->_user_id = session('visitor.user_id');
        $this->_integral_log_mod = M('integral_log');
    }

    /**
     * @api {get} /index.php?m=Home&c=Myintegral&a=integral_log_list 积分明细列表
     * @apiName 积分明细列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 积分明细列表
     * 
     * @apiSuccess {String} integral_id 积分日志ID
     * @apiSuccess {String} user_id 用户ID
     * @apiSuccess {String} user_name 用户名
     * @apiSuccess {String} point 积分
     * @apiSuccess {String} add_time 添加时间
     * @apiSuccess {String} remark 备注
     * @apiSuccess {String} integral_type 积分类型
     * @apiSuccess {String} integral_type_name 积分类型名称
     */
    function integral_log_list() {
        $integral_type = I('request.integral_type', 0, 'intval');
        if ($integral_type) {
            $map['integral_type'] = $integral_type;
        }
        $map['user_id'] = $this->_user_id;
        $integral_log_list = $this->_integral_log_mod->where($map)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        $integral_type_list = $this->_get_integral_type_list();
        if ($integral_log_list) {
            foreach ($integral_log_list as $key => $integral_log) {
                if($integral_log['integral_type'] == '5' || $integral_log['integral_type'] == '7' || $integral_log['integral_type'] == '8' || $integral_log['integral_type'] == '9'){
                    $integral_log_list[$key]['type'] = 'decrease';
                }else{
                    $integral_log_list[$key]['type'] = 'increase';
                }
                $integral_log_list[$key]['integral_type_name'] = $integral_type_list[$integral_log['integral_type']];
                $integral_log_list[$key]['add_time'] = date('Y-m-d H:i:s', $integral_log['add_time']);
            }
        }
        render('10000', '', array('integral_log_list' => $integral_log_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Myintegral&a=integral_goods_list 积分产品列表
     * @apiName 积分产品列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 积分产品列表
     * 
     * @apiSuccess {String} goods_id 积分产品ID
     * @apiSuccess {String} goods_name 积分产品名称
     * @apiSuccess {String} goods_logo 积分产品图片
     * @apiSuccess {String} goods_stock 积分产品库存
     * @apiSuccess {String} goods_price 积分产品价值
     * @apiSuccess {String} goods_point 所需积分
     * @apiSuccess {String} add_time 添加时间
     * @apiSuccess {String} goods_state 积分产品状态
     * @apiSuccess {String} sort_order 排序
     */
    function integral_goods_list() {
        $integral_goods_mod = M('integral_goods');
        $integral_goods_list = $integral_goods_mod
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        if ($integral_goods_list) {
            foreach ($integral_goods_list as $key => $integral_goods) {
                $integral_goods_list[$key]['add_time'] = date('Y-m-d H:i:s', $integral_goods['add_time']);
            }
        }
        
        render('10000', '', array('integral_goods_list' => $integral_goods_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Myintegral&a=integral_goods 积分产品详情
     * @apiName 积分产品详情
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 积分产品详情
     * 
     * @apiParam {String} goods_id 积分产品ID
     * 
     * @apiSuccess {String} goods_name 积分产品名称
     * @apiSuccess {String} goods_logo 积分产品图片
     * @apiSuccess {String} goods_stock 积分产品库存
     * @apiSuccess {String} goods_price 积分产品价值
     * @apiSuccess {String} goods_point 所需积分
     * @apiSuccess {String} add_time 添加时间
     * @apiSuccess {String} goods_state 积分产品状态
     * @apiSuccess {String} sort_order 排序
     */
    function integral_goods() {
        $goods_id = I('request.goods_id', 0, 'intval');
        if (empty($goods_id)) {
            render('10200', '积分产品不存在');
        }
        $integral_goods_mod = M('integral_goods');
        $map['goods_id'] = $goods_id;
        $integral_goods = $integral_goods_mod->where($map)->find();
        $integral_goods['add_time'] = date('Y-m-d H:i:s', $integral_goods['add_time']);
        if (empty($integral_goods)) {
            render('10200', '积分产品不存在');
        }
        render('10000', '', array('integral_goods' => $integral_goods));
    }

    /**
     * @api {get} /index.php?m=Home&c=Myintegral&a=integral_goods_log_add 添加积分兑换
     * @apiName 添加积分兑换
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 添加积分兑换
     * 
     * @apiParam {String} goods_id 积分产品ID
     * 
     * @apiParam {String} my_num 兑换数量
     * @apiParam {String} my_name 收件人
     * @apiParam {String} my_address 收件地址
     * @apiParam {String} my_mobile 收件电话
     * @apiParam {String} my_remark 备注
     * 
     */
    function integral_goods_log_add() {
        $goods_id = I('request.goods_id', 0, 'intval');
        if (empty($goods_id)) {
            render('10200', '积分产品不存在');
        }
        $integral_goods_mod = M('integral_goods');
        $map['goods_id'] = $goods_id;
        $integral_goods = $integral_goods_mod
                ->where($map)
                ->find();
        if (empty($integral_goods)) {
            render('10200', '积分产品不存在');
        }
        if ($integral_goods['goods_stock'] < 1) {
            render('10200', '积分产品数量不足');
        }
        if (IS_POST) {
            $my_num = I('post.my_num', 0, 'intval');
            if ($my_num < 1) {
                render('10200', '提交兑换数量错误');
            }
            if ($integral_goods['goods_stock'] < $my_num) {
                render('10200', '此积分产品没有足够的兑换数量');
            }

            $member_mod = M('member');
            $member = $member_mod->where(array('user_id' => $this->_user_id))->find();

            $point = $integral_goods['goods_point'] * $my_num;
            if ($point > $member['integral']) {
                render('10200', '用户没有足够的积分进行兑换');
            }

            $data = array(
                'goods_id'=>$goods_id,
                'goods_name'=>$integral_goods['goods_name'],
                'user_id' => $this->_user_id,
                'user_name' => $member['user_name'],
                'my_num' => $my_num,
                'my_name' => I('post.my_name', ''),
                'my_address' => I('post.my_address', ''),
                'my_mobile' => I('post.my_mobile', ''),
                'my_remark' => I('post.my_remark', ''),
                'add_time' => time(),
            );
            $integral_goods_log_mod = M('integral_goods_log');
            $integral_goods_log_mod->add($data);

            //更新兑换产品库存数量
            $integral_goods_log_mod->where(array('goods_id' => $goods_id))->save(array('goods_stock' => $integral_goods['goods_stock'] - $my_num, 'goods_stock_exchange' => $integral_goods['goods_stock_exchange'] + $my_num));

            //写入记录 扣除积分
            $member_mod->where(array('user_id' => $this->_user_id))->save(array('integral' => $member['integral'] - $point));

            //操作记录入积分记录
            $integral_log = array(
                'user_id' => $this->_user_id,
                'user_name' => $member['user_name'],
                'point' => $point,
                'add_time' => time(),
                'remark' => '兑换积分产品扣除积分' . $point,
                'integral_type' => INTEGRAL_GOODS,
            );
            $this->_integral_log_mod->add($integral_log);
            
            render('10000', '申请成功');
        }
    }
    
    /**
     * @api {get} /index.php?m=Home&c=Myintegral&a=integral_goods_log_list 积分产品兑换记录
     * @apiName 积分产品兑换记录
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 积分产品兑换记录
     * 
     * @apiSuccess {String} user_id 用户ID
     * @apiSuccess {String} user_name 用户名
     * @apiSuccess {String} goods_id 积分产品ID
     * @apiSuccess {String} goods_name 积分产品名称
     * @apiSuccess {String} my_num 兑换数量
     * @apiSuccess {String} my_name 收件人
     * @apiSuccess {String} my_address 收件地址
     * @apiSuccess {String} my_mobile 收件电话
     * @apiSuccess {String} my_remark 备注
     * @apiSuccess {String} wuliu_name 物流名称
     * @apiSuccess {String} wuliu_danhao 物流名称
     * @apiSuccess {String} state 状态
     * @apiSuccess {String} add_time 申请时间
     * 
     */
    function integral_goods_log_list()
    {
        $integral_goods_log_mod = M('integral_goods_log');
        $map['user_id'] = $this->_user_id;
        $integral_goods_log_list = $integral_goods_log_mod
                ->where($map)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        
        foreach ($integral_goods_log_list as $key => $integral_goods_log) {
            $integral_goods_log_list[$key]['add_time']= date('Y-m-d H:i:s', $integral_goods_log['add_time']);
        }
        render('10000', '',array('integral_goods_log_list'=>$integral_goods_log_list));
    }
    
    /**
     * @api {get} /index.php?m=Home&c=Myintegral&a=integral_type_list 积分日志类型
     * @apiName 积分日志类型
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 积分日志类型
     * 
     * @apiSuccess {String} key 积分日志类型ID
     * @apiSuccess {String} value 积分日志类型名称
     * 
     */
    function integral_type_list()
    {
        $integral_type_list = $this->_get_integral_type_list();
        $result = array(); 
        foreach ($integral_type_list as $key => $integral_type) {
            $result[] = array(
                'key' => $key,
                'value' => $integral_type,
            );
        }
        render('10000', '',array('integral_type_list'=>  $result));
    }
    
    

    
    function _get_integral_type_list() {
        return array(
            INTEGRAL_REG => "注册赠送积分", #注册赠送积分类型
            INTEGRAL_LOGIN => "登录赠送积分", #登录赠送积分类型
            INTEGRAL_RECOM => "推荐赠送积分", #推荐赠送积分类型
            INTEGRAL_BUY => "购买赠送积分", #购买赠送积分类型
            INTEGRAL_SELLER => "抵扣扣除积分", #抵扣扣除积分类型
            INTEGRAL_ADD => "管理员增加积分", #管理员增加积分
            INTEGRAL_SUB => "管理员减少积分", #管理员减少积分
            INTEGRAL_EGG => "砸金蛋扣除积分", #砸金蛋扣除积分类型
            INTEGRAL_GOODS => "兑换礼品扣除积分", #兑换礼品扣除积分类型
        );
    }

}
